<template>
  <div id="app">
    <el-dialog title="用户详情信息" :visible.sync="dialogVisible" width="40%" style="border-radius: 7px; font-weight: bold">
      <template slot-scope="scope">
        <div v-model="user" style="text-align: left; line-height: 37px;">
          <el-descriptions title="">
            <el-descriptions-item label="头像">
              <el-image :src="user.avatar" style="width: 53px; height: 53px;"></el-image>
            </el-descriptions-item>
            <el-descriptions-item label="用户名">{{user.userName}}</el-descriptions-item>
            <el-descriptions-item label="手机号">{{user.phone}}</el-descriptions-item>
            <el-descriptions-item label="邮箱">{{user.email}}</el-descriptions-item>
            <el-descriptions-item label="出生日期">{{user.birthday}}</el-descriptions-item>
            <el-descriptions-item label="上一次登陆时间">{{user.lastLoginTime}}</el-descriptions-item>
            <el-descriptions-item label="性别">
              <el-tag size="small">{{user.sex}}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="是否启用">
              <el-switch v-model="user.enable" :active-value="1" :inactive-value="0" disabled active-color="#32CD32"
                inactive-color="#A9A9A9">
              </el-switch>
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </template>
    </el-dialog>
    <div id="head">
      <el-row :gutter="24">
        <el-col :span="20">
          <el-col :span="6">
            <el-row :gutter="24">
              <el-col :span="21">
                <el-input size="medium" v-model="userName">
                  <template slot="prepend">用户名</template>
                </el-input>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="6">
            <el-row :gutter="24">
              <el-col :span="20">
                <el-input size="medium" v-model="phone">
                  <template slot="prepend">电话</template>
                </el-input>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="7">
            <el-row :gutter="24">
              <el-col :span="16">
                <el-select v-model="value" placeholder="选择爱好" size="medium">
                  <el-option v-for="item in options" :key="item.value" :label="item.text"
                    :value="item.text">
                  </el-option>
                </el-select>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" @click="getUserInfo()" size="small">查询</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="info" @click="clean" size="small">重置</el-button>
          </el-col>
        </el-col>
      </el-row>
    </div>
    <div id="main">
      <el-table :data="userInfo" :highlight-current-row="true"  size="medium" style="width: 100%" max-height="100%">
        <el-table-column prop="avatar" label="头像" width="78">
          <template slot-scope="scope">
            <img :src="scope.row.avatar" width="55" height="55" class="cover"  alt="加载失败"/>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="用户名"></el-table-column>
        <el-table-column prop="phone" label="电话"></el-table-column>
        <el-table-column prop="hobby" label="爱好"></el-table-column>
        <el-table-column prop="enable" label="状态"><template slot-scope="scope">
            <el-switch v-model="scope.row.enable" :active-value="1" :inactive-value="0" active-color="#32CD32"
              inactive-color="#A9A9A9" @change="disableUser(scope.row)">
            </el-switch></template>
        </el-table-column>
        <el-table-column prop="operatorName" label="操作人"></el-table-column>
        <el-table-column prop="lastLoginTime" label="上一次登录时间" width="200"></el-table-column>
        <el-table-column prop="modifyTime" label="修改时间" width="200"></el-table-column>
        <el-table-column label="操作"  fixed="right" width="300px">
          <template slot-scope="scope">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="getRow(scope.row)">详 情</el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="deleteUser(scope.row)">删 除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination background layout="prev, pager, next" :hide-on-single-page="true" :total="total"
        @current-change="setCurrentPage" :current-page="currentPage">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'user',
    data() {
      return {
        checked: [],
        userInfo: [],
        options: [],
        user: {},
        total: 0,
        userName: '',
        phone: '',
        value: '',
        dialogVisible: false,
        currentPage: 1
      }
    },
    created() {
      this.getUserInfo();
      this.getDict();
    },
    methods: {
      getRow(info) {
        this.dialogVisible = true;
        this.user = info;
      },
      disableUser(param) {
        this.$axios.post("/sys-user/update/status", {
          id: param.id,
          enable: param.enable
        }).then(res => {
          if (res.data.code === "200") {
            if (param.enable === 1) {
              this.$message({
                message: "启用用户成功",
                type: 'success'
              });
            }
            if (param.enable === 0) {
              this.$message({
                message: "禁用用户成功",
                type: 'success'
              });
            }
            this.getUserInfo();
          } else {
            if (param.enable === 1) {
              this.$message.error("启用用户失败");
            }
            if (param.enable === 0) {
              this.$message.error("禁用用户失败");
            }
          }
        })
      },
      deleteUser(param) {
        this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.post("/sys-user/delete/user", {
            id: param.id
          }).then(rel => {
            if (rel.data.code === "200") {
              this.$message({
                message: "删除用户成功",
                type: 'success'
              });
              this.getUserInfo();
            } else {
              this.$message.error("删除用户失败");
            }
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      setCurrentPage(param) {
        this.currentPage = param;
        this.getUserInfo();
      },
      clean() {
        this.userName = '';
        this.phone = '';
        this.value = '';
        this.getUserInfo();
      },
      getDict() {
        this.$axios.post('/dict-message/listByType', {
          type: 'hobby'
        }).then(res => {
          if (res.data.code === '200') {
            this.options = res.data.data;
          } else {
            this.$message.error(res.data.message)
          }
        });
      },
      getUserInfo() {
        this.$axios.post('/sys-user/user/list', {
          currentPage: this.currentPage,
          userName: this.userName,
          hobby: this.value,
          phone: this.phone
        }).then(res => {
          if (res.data.code === '200') {
            this.total = res.data.total;
            this.userInfo = res.data.data;
          } else {
            this.$message.error(res.data.message)
          }

        });
      },
    }
  }
</script>

<style scoped>
  #app {
    background-color: white;
    padding-left: 5px;
    padding-top: 5px;
    border-radius: 7px;
  }

  #head {
    padding-top: 11px;
  }

  #main {
    height: 580px;
    overflow-y: auto;
    padding-top: 41px;
    text-align: center;
    border-radius: 7px;
  }
</style>
